package org.project.product.android.pfe.fanta.domain;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.util.Utils;
import org.project.product.android.pfe_fanta.CategoryListActivity;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public class DeleteProductDialog extends Dialog {

	Product product;
	private final Context context;

	public DeleteProductDialog(Context context, Product product) {
		super(context);
		this.product = product;
		this.context = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.delete_product_dialog);

		findViewById(R.id.bt_cancel).setOnClickListener(
		        new View.OnClickListener() {

			        @Override
			        public void onClick(View v) {
				        dismiss();
			        }
		        });
		findViewById(R.id.bt_delete).setOnClickListener(
		        new View.OnClickListener() {

			        @Override
			        public void onClick(View v) {
				        deleteProduct();
			        }
		        });

	}

	public void deleteProduct() {
		EditText quantityField = (EditText) findViewById(R.id.edit_quantity_number);
		int quantity;
		try {
			quantity = Integer.parseInt(quantityField.getText().toString());
		} catch (Exception exception) {
			quantity = 1;
		}
		if (quantity > product.getQuantity()) {
			Toast.makeText(
			        context,
			        "La quantite a supprimer doit etre inferieur à la quantits du produit ",
			        Toast.LENGTH_LONG).show();
		} else {
			DataManagerFacade.getInstance(getOwnerActivity()).deleteProduct(
			        product, quantity);

			Utils.backHome(context, CategoryListActivity.class);
		}

	}
}
